package com.clementheliou.cheliou.business.dataaccess.photo.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.clementheliou.cheliou.business.dataaccess.photo.PhotoDataAccessService;
import com.clementheliou.cheliou.dal.dao.photo.PhotoDAO;
import com.clementheliou.cheliou.dal.model.base.enumeration.SortOrder;
import com.clementheliou.cheliou.dal.model.photo.Photo;
import com.clementheliou.cheliou.dal.model.photo.dto.PhotoDTO;
import com.clementheliou.cheliou.dal.model.photo.enumeration.PhotoSortableColumn;

/**
 * {@link PhotoDataAccessService} that used the default implementation provided by the model's
 * layer. It should not be used if many implementations are availables in this last one.
 * <p>
 * Due to the {@link Transactional} annotation and the value of its <i>readOnly</i> attribute, all
 * methods are called in a transaction that will be rollbacked in the end. This behavior can be
 * overriden at the method level, more particularly if you need to perform an insert, update or
 * delete action.
 * </p>
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @see {@link PhotoDataAccessService};
 * @see {@link Service};
 * @see {@link Transactional}.
 * @since 1.0
 */
@Service
@Transactional(readOnly = true)
public class PhotoDefaultDataAccessService implements PhotoDataAccessService {

	/**
	 * Logger for this class.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Logger};
	 * @see {@link LoggerFactory}.
	 * @since 1.0
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(PhotoDefaultDataAccessService.class);

	/**
	 * DAO used to manage the photos persistence.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Autowired};
	 * @see {@link PhotoDAO}.
	 * @since 1.0
	 */
	@Autowired
	private PhotoDAO photoDAO;

	/**
	 * <i>{@inheritDoc}</i>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Override
	public Photo getPhotoById(Integer id) {
		LOGGER.debug("Get photo with {} id.", id);
		return photoDAO.getEntity(id);
	}

	/**
	 * <i>{@inheritDoc}</i>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Override
	public List<PhotoDTO> getLastPhotos(int first, int pageSize, PhotoSortableColumn sortableColumn, SortOrder sortOrder) {
		LOGGER.debug("Get the last photos from the {} index with {} results at the most, ordered by '{}' {}.", first, pageSize, sortableColumn.getColumnName(), sortOrder.getHqlClause());
		return photoDAO.getLastPhotos(first, pageSize, sortableColumn, sortOrder);
	}

	/**
	 * <i>{@inheritDoc}</i>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Override
	public Integer countPhotos() {
		LOGGER.debug("Count the photos.");
		return photoDAO.count();
	}

}
